<%= form_for @saved_filter, id:"search_form"  do |f| %>
  <% if @saved_filter.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@saved_filter.errors.count, "error") %>
        prohibited this result from being saved:</h2>
      <ul>
        <% @saved_filter.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <h4>Filter Details</h4>
  <%= label_tag do %>Name
    <%= f.text_field :name %>
  <% end %>
  <div id="subscribers">
    <%= f.label :subscribers %><br />
    <%= f.hidden_field "subscriber_list", {value: @saved_filter.subscriber_list, class: "remote_select2", "data-multiple"=>true, "data-id-attribute"=>"email" , "data-parse-initial"=>"true", "data-create-new"=>"true", "data-object"=>"users", "data-attribute"=>"email" , "data-path"=>users_path} %>
  </div>
  <%= f.check_box :public %>
  <%= label_tag "Share publicly?" %><br/>
  <%= f.hidden_field :saved_filter_type %>
  <h4>Filter Criteria</h4>
  <%= fields_for @q do |f2| %>

    <%= render partial: "#{f.object.saved_filter_type.downcase.pluralize}/search_fields", locals: {f: f2}%>
  <% end %>

  <h4>Index Columns</h4>
  <% if f.object.try(:saved_filter_type).try(:downcase) == "result" %>
    <%= f.check_box :store_index_columns, {}%>
    <%= label_tag "Save Columns?" %><br/>


    <%= label_tag do %>Built-in Fields 
      
      <%= hidden_field_tag "columns[built_in]", @index_columns.select{|c| !c.to_s.starts_with("metadata:")}.join(", "), {class: 'select2-tags', multiple:true, data:{tags: Result.valid_column_names}} %>


    <% end %>
    
    <%= label_tag do %>Metadata <span data-tooltip aria-haspopup="true" class="has-tip tip-left" title="Show metadata fields.<br/>Nested fields can be displayed by separating with ':'.<br/> Examples: 'status_code'<br/>'scan:latest:timestamp'<br/> Note: The same value can be used in the metadata search field to filter to only results containing a value for this field. Displaying fields containing large amounts of data could significantly impact performance."><i class="fi-info"></i></span>
      <%= hidden_field_tag "columns[metadata_fields]", @index_columns.select{|c| c.to_s.starts_with("metadata:")}.map{|c| c.gsub("metadata:", "")}.join(", ") , {class: "select2-tags", multiple: true} %> 
    <% end %>

  <% end %>

  <%= f.submit class: "button small" %>
  <%#= f.button "Reset", type: :reset, class:"secondary" %>
<% end %>
